package org.jeecg.modules.basic.repair.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.basic.repair.entity.LwgyDepreRecord;
import org.jeecg.modules.basic.repair.entity.LwgyFixed;
import org.jeecg.modules.basic.repair.entity.LwgyFixedAssets;
import org.jeecg.modules.basic.repair.entity.PropertyReport;
import org.jeecg.modules.basic.repair.service.ILwgyDepreRecordService;
import org.jeecg.modules.basic.repair.service.ILwgyDepreTypeService;
import org.jeecg.modules.basic.repair.service.ILwgyFixedAssetsService;
import org.jeecg.modules.basic.repair.service.ILwgyFixedService;
import org.jeecg.modules.basic.repair.vo.LwgyFixedAssetsPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @Description: 固定资产
* @Author: jeecg-boot
* @Date:   2023-02-08
* @Version: V1.0
*/
@Api(tags="固定资产")
@RestController
@RequestMapping("/fixed/lwgyFixed")
@Slf4j
public class LwgyFixedController extends JeecgController<LwgyFixed, ILwgyFixedService> {
   @Autowired
   private ILwgyFixedService lwgyFixedService;

    @Autowired
    private ILwgyDepreRecordService lwgyDepreRecordService;


   /**
    * 分页列表查询
    *
    * @param
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "固定资产-分页列表查询")
   @ApiOperation(value="固定资产-分页列表查询", notes="固定资产-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(LwgyFixed lwgyFixed,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<LwgyFixed> queryWrapper = QueryGenerator.initQueryWrapper(lwgyFixed, req.getParameterMap());


       Page<LwgyFixed> page = new Page<>(pageNo, pageSize);
       IPage<LwgyFixed> pageList = lwgyFixedService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

 /*  *//**
    *   添加
    *
    * @param
    * @return
    *//*
   @AutoLog(value = "固定资产-添加")
   @ApiOperation(value="固定资产-添加", notes="固定资产-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody LwgyFixed lwgyFixed) {
       String assets_no="";
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
               "yyyyMMddHHmmss-SSS");
       assets_no=simpleDateFormat.format(new Date());
           lwgyFixed.setAssetsNo(assets_no);
           lwgyFixed.setDestoryStatus("0");
           lwgyFixedService.save(lwgyFixed);
           return Result.OK("添加成功！");
   }

   *//**
    *  编辑
    *
    * @param lwgyFixed
    * @return
    *//*
   @AutoLog(value = "固定资产-编辑")
   @ApiOperation(value="固定资产-编辑", notes="固定资产-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody LwgyFixed lwgyFixed) {
       lwgyFixed.setDestoryStatus("1");
       lwgyFixedService.updateById(lwgyFixed);
       return Result.OK("编辑成功!");
   }*/

    @AutoLog(value = "固定资产-折算")
    @ApiOperation(value="固定资产-折算", notes="固定资产-折算")
    @PostMapping(value = "/updateConversion")
    public Result<?> updateConversion(@RequestBody LwgyFixed lwgyFixed) {

      /*  lwgyFixedService.updateById(lwgyFixed);
        return Result.OK("折算成功!");*/
        if ("1".equals(lwgyFixed.getDestroyId())) {  // 双倍余额递减法
            lwgyFixed.calculateDepreciationByDoubleDecline();
        } else if ("2".equals(lwgyFixed.getDestroyId())) {  // 工作量法
            lwgyFixed.calculateDepreciationByWorkload();
        } else if ("3".equals(lwgyFixed.getDestroyId())) {  // 平均年限法
            lwgyFixed.calculateDepreciationByAverageYear();
        } else if ("4".equals(lwgyFixed.getDestroyId())) {  // 年度总和法
            lwgyFixed.calculateDepreciationBySumOfYearsDigits();
        }

        // 更新到数据库中
        lwgyFixedService.updateById(lwgyFixed);

        return Result.OK("折算成功!");
    }

    /**
     *   添加
     *
     * @param
     * @return
     */
    @AutoLog(value = "-添加")
    @ApiOperation(value="-添加", notes="-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody LwgyFixedAssetsPage lwgyFixedAssetsPage) {
        LwgyFixed lwgyFixed = new LwgyFixed();
        String assets_no="";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyyMMddHHmmss-SSS");
        assets_no=simpleDateFormat.format(new Date());
        lwgyFixed.setAssetsNo(assets_no);
        lwgyFixed.setDestoryStatus("0");
        BeanUtils.copyProperties(lwgyFixedAssetsPage, lwgyFixed);
        lwgyFixedService.saveMain(lwgyFixed, lwgyFixedAssetsPage.getLwgyDepreRecordList());
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param
     * @return
     */
    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody LwgyFixedAssetsPage lwgyFixedAssetsPage) {
        LwgyFixed lwgyFixed = new LwgyFixed();
        BeanUtils.copyProperties(lwgyFixedAssetsPage, lwgyFixed);
        LwgyFixed lwgyFixedEntity = lwgyFixedService
                .getById(lwgyFixed.getId());
        if(lwgyFixedEntity==null) {
            return Result.error("未找到对应数据");
        }
        lwgyFixedService.updateMain(lwgyFixed, lwgyFixedAssetsPage.getLwgyDepreRecordList());
        return Result.OK("编辑成功!");
    }



    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id删除")
    @ApiOperation(value="通过id删除", notes="通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       lwgyFixedService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.lwgyFixedService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        LwgyFixed lwgyFixed = lwgyFixedService.getById(id);
        if(lwgyFixed==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(lwgyFixed);

    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "打分指标通过主表ID查询")
    @ApiOperation(value="打分指标主表ID查询", notes="打分指标-通主表ID查询")
    @GetMapping(value = "/queryLwgyTargetTypeByMainId")
    public Result<?> queryLwgyTargetTypeListByMainId(@RequestParam(name="id",required=true) String id) {
        List<LwgyDepreRecord> lwgyDepreRecordList =  lwgyDepreRecordService.selectByMainId(id);
        return Result.OK(lwgyDepreRecordList);
    }

  /* *//**
    *   通过id删除
    *
    * @param id
    * @return
    *//*
   @AutoLog(value = "固定资产-通过id删除")
   @ApiOperation(value="固定资产-通过id删除", notes="固定资产-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       lwgyFixedService.removeById(id);
       return Result.OK("删除成功!");
   }

   *//**
    *  批量删除
    *
    * @param ids
    * @return
    *//*
   @AutoLog(value = "固定资产-批量删除")
   @ApiOperation(value="固定资产-批量删除", notes="固定资产-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.lwgyFixedService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   *//**
    * 通过id查询
    *
    * @param id
    * @return
    *//*
   @AutoLog(value = "固定资产-通过id查询")
   @ApiOperation(value="固定资产-通过id查询", notes="固定资产-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       LwgyFixed lwgyFixed = lwgyFixedService.getById(id);
       if(lwgyFixed==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(lwgyFixed);
   }
*/
   /**
   * 导出excel
   *
   * @param request
   * @param
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, LwgyFixed lwgyFixed) {
       return super.exportXls(request, lwgyFixed, LwgyFixed.class, "固定资产");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, LwgyFixed.class);
   }


   


}
